/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.texture;

import com.android1.amarena2d.commons.Size;
import com.android1.amarena2d.engine.EngineObject;
import com.android1.amarena2d.engine.Resource;
import com.badlogic.gdx.math.Vector2;

//todo
public class AtlasTexturedMeshImpl extends EngineObject implements AtlasTexturedMesh {

    protected final int index;

    protected final AtlasMesh atlasMesh;
    protected final AtlasTextureFrame atlasTextureFrame;
    protected final Size size = new Size();
    protected final Vector2 pos = new Vector2();

    protected boolean flipX;
    protected boolean flipY;

    public AtlasTexturedMeshImpl(final AtlasMesh atlasMesh, AtlasTextureFrame textureFrame) {
        this.atlasMesh = atlasMesh;
        this.atlasTextureFrame = textureFrame;
        this.index = textureFrame.getIndex();
    }

    @Override
    public AtlasMesh getAtlasMesh() {
        return atlasMesh;
    }

    @Override
    public int getIndex() {
        return index;
    }

    @Override
    public void render() {
        atlasMesh.render(index);
    }

    @Override
    public float getWidth() {
        return size.width;
    }

    @Override
    public float getHeight() {
        return size.height;
    }

    @Override
    public void dispose() {
        atlasMesh.dispose();
    }

    @Override
    public void init() {
        atlasMesh.init();
    }

    @Override
    public boolean isInit() {
        return atlasMesh.isInit();
    }


    @Override
    public void obtainRef(Consumer consumer) {
        atlasMesh.obtainRef(consumer);
    }

    @Override
    public void releaseRef(Consumer consumer) {
        atlasMesh.releaseRef(consumer);
    }

    @Override
    public void releaseAllRefs() {
        atlasMesh.releaseAllRefs();
    }

    @Override
    public boolean hasRefs() {
        return atlasMesh.hasRefs();
    }

    @Override
    public void register(StateListener listener) {
        atlasMesh.register(listener);
    }

    @Override
    public void unregister(StateListener listener) {
        atlasMesh.unregister(listener);
    }

    @Override
    public void markPersistent() {
        atlasMesh.markPersistent();
    }

    @Override
    public void revokePersistent() {
        atlasMesh.revokePersistent();
    }

    @Override
    public boolean isEagerDispose() {
        return atlasMesh.isEagerDispose();
    }

    @Override
    public void setEagerDispose(boolean dispose) {
        atlasMesh.setEagerDispose(dispose);
    }

    @Override
    public boolean equalResource(Resource otherResource) {
        return otherResource instanceof AtlasTexturedMesh && ((AtlasTexturedMesh) otherResource).getAtlasMesh().equalResource(getAtlasMesh());
    }

    @Override
    public SharedTexturedMesh copy() {
        SharedTexturedMesh texturedMesh = new SharedTexturedMesh(getManagedTexture(), atlasTextureFrame.getX(), atlasTextureFrame.getY(), atlasTextureFrame.getWidth(), atlasTextureFrame.getHeight());
        texturedMesh.setFlipX(isFlipX());
        texturedMesh.setFlipY(isFlipY());
        texturedMesh.setSize(size.getWidth(), size.getHeight());
        return texturedMesh;
    }

    @Override
    public AtlasTextureFrame getTextureFrame() {
        return atlasTextureFrame;
    }

    @Override
    public void setTextureFrame(TextureFrame textureFrame) {
        throw new UnsupportedOperationException();
    }

    @Override
    public ManagedTexture getManagedTexture() {
        return atlasMesh.getManagedTexture();
    }

    @Override
    public boolean isDirty() {
        return atlasMesh.isDirty();
    }

    @Override
    public Size getSize() {
        return size;
    }

    @Override
    public void setSize(final float width, final float height) {
        this.size.set(width, height);
        atlasMesh.setDirty();
    }

    @Override
    public void setWidth(float width) {
        this.size.setWidth(width);
        atlasMesh.setDirty();
    }

    @Override
    public void setHeight(float height) {
        this.size.setHeight(height);
        atlasMesh.setDirty();
    }

    public boolean isFlipX() {
        return flipX;
    }

    public void setFlipX(boolean flipX) {
        this.flipX = flipX;
        atlasMesh.setDirty();
    }

    public boolean isFlipY() {
        return flipY;
    }

    public void setFlipY(boolean flipY) {
        this.flipY = flipY;
        this.flipY = flipY;
    }

    @Override
    public float getX() {
        return pos.x;
    }

    @Override
    public float getY() {
        return pos.y;
    }

    @Override
    public void setX(float x) {
        pos.x = x;
        atlasMesh.setDirty();
    }

    @Override
    public void setY(float y) {
        pos.y = y;
        atlasMesh.setDirty();
    }

    @Override
    public void setXY(float x, float y) {
        pos.x = x;
        pos.y = y;
        atlasMesh.setDirty();
    }

    @Override
    public Vector2 getXY() {
        return pos;
    }


}